Back to course home
0% completed
Vote For New Content
Meta Hard Problems
Sliding Window Median (hard)
Solution: Sliding Window Median
Alien Dictionary (hard)
Solution: Alien Dictionary
Minimum Window Substring (hard)
Solution: Minimum Window Substring
Best Meeting Point (hard)
Solution: Best Meeting Point
Maximum Sum of 3 Non-Overlapping Subarrays (hard)
Solution: Maximum Sum of 3 Non-Overlapping Subarrays
Making A Large Island (hard)
Solution: Making A Large Island
Basic Calculator (Hard)
Solution: Basic Calculator
Serialize and Deserialize Binary Tree (hard)
Solution: Serialize and Deserialize Binary Tree
Longest Increasing Path in a Matrix (hard)
Solution: Longest Increasing Path in a Matrix
Valid Palindrome III (hard)
Solution: Valid Palindrome III
Nested List Weight Sum (medium)
Problem Statement
You are given a nestedList. Each element of the nestedList contains either integers or other such nested lists.
The depth level is determined by how many levels of nested lists the integer is within. For example, each element of the list [1, 1, [2], [2, [3]]] represents the depth of itself.
Return the sum of all integers of the list after multiplying each integer with its depth level.
Examples
-
Example 1:
- Input:
[[2, [3, 4]], [5, 6], 7] - Expected Output:
54 - Justification: The sum is calculated as 4 (2 at depth 2) + 9 (3 at depth 3) + 12 (4 at depth 3) + 10 (5 at depth 2) + 12 (6 at depth 2) + 7 (7 at depth 1).
- Input:
-
Example 2:
- Input:
[1, [4, [6]]] - Expected Output:
27 - Justification: The sum is 1 (depth 1) + 8 (4 at depth 2) + 18 (6 at depth 3).
- Input:
-
Example 3:
- Input:
[5, [2, [3, [4]]], 1] - Expected Output:
35 - Justification: The calculation is 5 (depth 1) + 4 (2 at depth 2) + 9 (3 at depth 3) + 16 (4 at depth 4) + 1 (depth 1).
- Input:
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Basic Calculator II
Solution: Nested List Weight Sum
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself